package com.mdp.workflow.bus.listener.exec;

import com.mdp.workflow.api.FlowFormDataService;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 执行监听器
 * 类 ： 默认
 * 事件： start
 * 表达式： 默认
 * 委托表达式：  ${formDataFlowStartTaskListener}
 * 表单数据状态更新  执行监听器，可以单独建立一个服务节点，更改其执行监听器，监听start事件，也可以更改人工节点的执行监听器
 * 初始化时为0 ，启动时更新位 1 审核中，结束时更新位审核通过2，如果未通过更新为审核未通过3
 * @author qqkj
 *
 */
@Service(value="formDataFlowStartTaskListener")
public class FormDataFlowStartTaskListener implements ExecutionListener {

	private static final long serialVersionUID = -7005384146965306816L;
	
	@Autowired
	private FlowFormDataService procinstFormDataService;

	@Override
	public void notify(DelegateExecution exec) {
		String agree=(String) exec.getVariable("agree");
		String processInstanceId=exec.getProcessInstanceId();
		String flowState="1";
		FlowElement element=exec.getCurrentFlowElement(); 
		if(element instanceof UserTask) {
			UserTask task=(UserTask)element;
			if(agree!=null ) {
				if(agree.equals("1")) {
					flowState="2";
					procinstFormDataService.updateFlowState(processInstanceId, flowState);
				}else if(agree.equals("0")) {
					flowState="3";
					procinstFormDataService.updateFlowState(processInstanceId, flowState);
				}else {
					//flowState="2";
					//procinstFormDataService.updateFlowState(processInstanceId, flowState);
				}
			}else {
			}
		}else {
			flowState="2";
			procinstFormDataService.updateFlowState(processInstanceId, flowState);
		}

	}

}
